package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.enviaEmail;
import bean.OSBean;
import bean.clienteBean;
import conexao.Conexao;
import dao.OSDao;
import dao.clienteDao;

public class detalheOsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public detalheOsServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		Connection conn = null;
		try {
			conn = new Conexao().getConexao();
		} catch (SQLException e) {
			new enviaEmail().enviar(e);
			out.print("falhaConexao");
			return;
		}

		try {
			Integer id = Integer.parseInt(request.getParameter("id"));
			
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
			SimpleDateFormat fd = new SimpleDateFormat("dd/MM/yyyy");
			
			OSBean beanos = new OSBean();
			beanos.setId(id);
			OSBean osBean;
			osBean = new OSDao().getOS(conn, beanos);
			osBean.setDataEntradaStr(df.format(osBean.getDataEntrada()));
			if(osBean.getDataAprovado() != null){
				osBean.setDataAprovadoStr(df.format(osBean.getDataAprovado()));
			}else{
				osBean.setDataAprovadoStr(" ");
			}
			if(osBean.getDataFinalizado() != null){
				osBean.setDataEntregaStr(df.format(osBean.getDataEntrega()));
			}else{
				osBean.setDataEntregaStr(" ");
			}

			clienteBean beancli = new clienteBean();
			beancli.setId(osBean.getIdCliente());
			clienteBean clibean = new clienteDao().getcliente(conn, beancli);
			clibean.setDataStr(fd.format(clibean.getData()));
			
			out.println("<?xml version='1.0' encoding='utf-8' ?>");
			out.println("<os>");
			out.print("<idos>");
			out.print(osBean.getId());
			out.print("</idos>");
			out.print("<secRes>");
			out.print(osBean.getNomeSecretario());
			out.print("</secRes>");
			out.print("<situacao>");
			out.print(osBean.getSituacao());
			out.print("</situacao>");
			out.print("<desRes>");
			if(osBean.getNomeDesigner() != null){
				out.print(osBean.getNomeDesigner());
			}
			out.print("</desRes>");
			out.print("<dataEntrada>");
			out.print(osBean.getDataEntradaStr());
			out.print("</dataEntrada>");
			out.print("<dataAprovacao>");
			out.print(osBean.getDataAprovadoStr());
			out.print("</dataAprovacao>");
			out.print("<dataEntrega>");
			out.print(osBean.getDataEntregaStr());
			out.print("</dataEntrega>");
			out.print("<cliente>");
			out.print(clibean.getNome());
			out.print("</cliente>");
			out.print("<endereco>");
			out.print(clibean.getEndereco());
			out.print("</endereco>");
			out.print("<bairro>");
			out.print(clibean.getBairro());
			out.print("</bairro>");
			out.print("<cidade>");
			out.print(clibean.getNomeCidade());
			out.print("</cidade>");
			out.print("<cpfCnpj>");
			if (clibean.getCpf().isEmpty()) {
				out.print(clibean.getCnpj());
			} else {
				out.print(clibean.getCpf());
			}
			out.print("</cpfCnpj>");
			out.print("<telefone>");
			out.print(clibean.getTelefone());
			out.print("</telefone>");
			out.print("<celular>");
			out.print(clibean.getCelular());
			out.print("</celular>");
			out.print("<dataCadastro>");
			out.print(clibean.getDataStr());
			out.print("</dataCadastro>");
			out.print("<descricao>");
			out.print(osBean.getDescricao());
			out.print("</descricao>");
			out.println("</os>");
			
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (Exception ex) {
				new enviaEmail().enviar(ex);
				ex.printStackTrace();
			}
			new enviaEmail().enviar(e);
			throw new RuntimeException(e);
		} finally {
			try {
				conn.close();
			} catch (Exception ex) {
				new enviaEmail().enviar(ex);
			}
		}

	}

}
